import { Injectable, Inject } from '@nestjs/common';
import { PrismaService } from "src/prisma/prisma.service";

@Injectable()
export class FavoriteService {
    @Inject(PrismaService)
    private prismaService: PrismaService;

    async del(id: number) {
        return this.prismaService.favorite.deleteMany({
            where: {
                id
            }
        })
    }
    async add(userId: number, chatHistoryId: number) {
        return this.prismaService.favorite.create({
            data: {
                userId,
                chatHistoryId
            }
        })
    }
    async list(userId: number) {
        const favorites = await this.prismaService.favorite.findMany({
            where: {
                userId
            }
        })
        const res = [];
        for(let i=0; i<favorites.length; i++) {
            const chatHistory = await this.prismaService.chatHistory.findUnique({
                where: {
                    id: favorites[i].chatHistoryId
                }
            })
            res.push({ ...favorites[i], chatHistory })
        }

        return res;
    }
}
